/**
**  采集 获取163 网易新闻
*/
const https = require('https');

const cheerio = require('cheerio');
const Content = require('../../models/Content');
const Category = require('../../models/Category');

// const Tag = require('../../models/Tag.js');
// var _ = require('lodash');

/*
*  采集接口
* */
// let urlList = [
//     'https://3g.163.com/touch/reconstruct/article/list/BD21K0DLwangning/0-10.html', // 轻松一刻
//     'https://3g.163.com/touch/reconstruct/article/list/CQ9UDVKOwangning/0-10.html', // 胖编怪聊
//     'https://3g.163.com/touch/reconstruct/article/list/CQ9UJIJNwangning/0-10.html', // 曲一刀
// ];

const categoryName = '网易';

const mapUrlList = [
  {
    url: 'https://m.163.com/touch/reconstruct/article/list/BD21K0DLwangning/0-10.html',
    name: '轻松一刻',
    value: 'BD21K0DLwangning',
    suffix: 'BD21K0DLwangning',
  },
  // {
  //   url: 'https://m.163.com/touch/reconstruct/article/list/CQ9UJIJNwangning/0-10.html',
  //   name: '曲一刀',
  //   value: 'CQ9UJIJNwangning',
  //   suffix: 'CQ9UJIJNwangning',
  // },
];

/**
 * 开始采集
 */
const startGet_163_Text = async () => {
  for (let i = 0; i < mapUrlList.length; i++) {
    const item = mapUrlList[i];
    const { url } = item;
    let _categoryId = '';
    const _categoryName = categoryName;
    const findAtCategory = await Category.findOne({
      name: _categoryName,
    });
    // console.log('findAtCategory 163js', findAtCategory);
    if (findAtCategory) {
      _categoryId = findAtCategory._id;
      if (_categoryId) {
        https.get(url, (res) => {
          // 分段返回的 自己拼接
          let html = '';
          // 有数据产生的时候 拼接
          res.on('data', (chunk) => {
            html += chunk;
          });
          // 拼接完成
          res.on('end', () => {
            //   console.log(html);
            if (html) {
              booksQuery(html, _categoryId, item.suffix);
            }
          });
        });
      }
    } else {
      // console.log(_categoryName, '_categoryName');
      await new Category({
        name: _categoryName,
      }).save();
    }
  }
};

function booksQuery(body, _categoryId, contentKey) {
  let string = '';
  if (typeof body === 'string') {
    if (body.includes('artiList(')) {
      string = eval(body);
    }
  }
  if (string === '') {
    return;
  }

  // let key = mapCategoryNameToKey.find(item => item.name == _categoryName).value;
  const canGetSourceList = string[contentKey]; // 能采集的资源列表
  getEveryNews(canGetSourceList, _categoryId);
}

function getEveryNews(arcList = [], categoryId) {
  // 获取列表页的 文章列表，然后每篇独自采集
  if (arcList.length == 0) return;

  arcList.forEach((newsItem, index) => {
    if (index > 5) return; // 本次不采集10遍以上
    if (!newsItem.url) return;
    const content = {
      thumb: newsItem.imgsrc,
      docid: newsItem.docid,
      description: newsItem.digest,
      originCreateAt: newsItem.ptime,
    };
    // console.log('url', newsItem.url);
    // console.log('newsItem', newsItem);

    // "".startsWith
    if (newsItem.url.startsWith('https://3g.')) {
      newsItem.url = newsItem.url.replace('https://3g.', 'https://m.');
    }
    https.get(newsItem.url, (res) => {
      // 分段返回的 自己拼接
      let html = '';
      // 有数据产生的时候 拼接
      res.on('data', (chunk) => {
        html += chunk;
      });
      // 拼接完成
      res.on('end', async () => {
        // console.log(html);
        if (html) {
          $ = cheerio.load(html, { decodeEntities: false });
          content.title = $('article h1.article-title').text().trim();
          content.description = $('article .content').text().replace(/\s+/g, '');

          // 关键词是否有bug待定
          // content.keywords = getKeyWord(content.title, 2).join(',');

          if (content.description.length > 72) {
            content.description = content.description.substring(0, 72);
          }
          content.category = categoryId;
          content.content = $('article .content').html() || $('article .article-body ').html();
          content.originalLink = newsItem.url;
          // console.log("content",content)
          if (!content.title) return;
          const _condition = {
            $or: [{ title: content.title }, { docid: content.docid }],
          };
          try {
            const isHasContent = await Content.findOne(_condition);
            // console.log('isHasContent', content);
            if (!isHasContent) {
              new Content(content).save();
            }
          } catch (error) {

          }
        }
      });
    });
  });
}

function artiList(body) {
  return body;
}

exports.startGet_163_Text = startGet_163_Text;
